package user

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	//"github.com/gogf/gf-demo-user/v2/internal/model"
	"github.com/gogf/gf-demo-user/v2/internal/model/entity"
	"github.com/gogf/gf-demo-user/v2/api/web/v1"
	"github.com/gogf/gf-demo-user/v2/internal/service"
)

func (c *ControllerV1) Job(ctx context.Context, req *v1.JobReq) (res *v1.JobRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var Job *entity.Job
	err = g.Model("Job").Where("Job_id=?", req.Job_id).Scan(&Job)
	fmt.Println(err)
	res = &v1.JobRes{
		Job,
	}
	return
}

func (c *ControllerV1) JobPost(ctx context.Context, req *v1.JobPostReq) (res *v1.JobPostRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("JobQuestion").Data(g.Map{
		"id" : newUUID,
		"title" : req.Title,
		"company" : req.Company,
		"tag" : req.Tag,
		"description" : req.Description,
		"salary" : req.Salary,
		"isRemote" : req.IsRemote,
		"isHybrid" : req.IsHybrid,
		"expiry" : req.Expiry,
		"question" : req.Question,
		"status" : req.Status,
	}).Insert()
	fmt.Println(id)
	return
}

func (c *ControllerV1) JobDele(ctx context.Context, req *v1.JobDeleReq) (res *v1.JobDeleRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("JobQuestion").Where("Job_id=?", account.Id).Delete()
	fmt.Println(id)
	return
}

func (c *ControllerV1) JobQuestionDele(ctx context.Context, req *v1.JobQuestionDeleReq) (res *v1.JobDeleRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("JobQuestion").Where("Job_id=?", account.Id).Delete()
	fmt.Println(id)
	return
}

func (c *ControllerV1) JobQuestion(ctx context.Context, req *v1.JobQuestionReq) (res *v1.JobQuestionRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var user *entity.JobQuestion
	err = g.Model("JobQuestion").Where("Job_id=?", account.Id).Scan(&user)
	fmt.Println(err)
	res = &v1.JobQuestionRes{
		user,
	}
	return
}

func (c *ControllerV1) JobUp(ctx context.Context, req *v1.JobUpReq) (res *v1.JobUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("JobQuestion").Data(g.Map{
		"title" : req.Title,
		"company" : req.Company,
		"tag" : req.Tag,
		"description" : req.Description,
		"salary" : req.Salary,
		"isRemote" : req.IsRemote,
		"isHybrid" : req.IsHybrid,
		"expiry" : req.Expiry,
		"question" : req.Question,
		"status" : req.Status,
	}).Where("id", req.Job_id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) JobQuestionUp(ctx context.Context, req *v1.JobQuestionUpReq) (res *v1.JobQuestionUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("JobQuestion").Data(g.Map{
		"status" : req.Status,
		"expiry" : req.Expiry,
	}).Where("id", req.Job_id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) JobQuestionPost(ctx context.Context, req *v1.JobQuestionPostReq) (res *v1.JobQuestionUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("JobQuestion").Data(g.Map{
		"id" : newUUID,
		"question" : req.Question,
	}).Insert()
	fmt.Println(id)
	return
}




